package com.indusfo.spc.pda.service.impl;

import com.indusfo.spc.pda.mapper.CallmatDosingMapper;
import com.indusfo.spc.pda.pojo.CallmatDosing;
import com.indusfo.spc.pda.pojo.CallmatDto;
import com.indusfo.spc.pda.service.CallmatDosingService;
import com.indusfo.spc.pojo.Mat;
import com.indusfo.spc.vo.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CallmatDosingServiceImpl
 * @Description
 * @Author louk
 * @Date 2019/12/20 11:07
 */
@Service
public class CallmatDosingServiceImpl implements CallmatDosingService {
    private static final Logger logger= LoggerFactory.getLogger(CallmatDosingServiceImpl.class);
    @Resource
    CallmatDosingMapper callmatDosingMapper;
    /**
     * 新增超市叫料
     * @param callmatDosing
     * @return
     */
    @Override
    public JSONObject insertCallmatDosing(CallmatDosing callmatDosing) {
        //检查参数传递
        String checkResult = checkParam(callmatDosing);
        if(!StringUtils.isBlank(checkResult)){
            return JSONObject.build(JSONObject.ERROR,checkResult);
        }
        //判断当前表中是否存在指定物料的叫料信息,如果存在进行更新,不存在则进行新增
        CallmatDosing matCallmatDosing = callmatDosingMapper.queryCallmatDosing(callmatDosing);
        if(matCallmatDosing==null || matCallmatDosing.getCallmatDosingId()==null || "".equals(matCallmatDosing.getCallmatDosingId()) || matCallmatDosing.getDosingState()==2){
            int insert = callmatDosingMapper.insert(callmatDosing);
            if(insert>0){
                /*CallmatDto callmatDto = callmatDosingMapper.callMatDoSingSuccess(callmatDosing.getMatId());
                callmatDto.setDemandNum(callmatDosing.getDemandNum());*/
                return JSONObject.oK("叫料成功");
            }
        }else{
            matCallmatDosing.setDemandNum(String.valueOf(Integer.parseInt(matCallmatDosing.getDemandNum())+Integer.parseInt(callmatDosing.getDemandNum())));
            int update = callmatDosingMapper.updateCallmatDosing(matCallmatDosing);
            if(update>0){
                return JSONObject.oK("叫料成功");
            }
        }

        return JSONObject.build(JSONObject.ERROR,"网络错误,新增失败",null);
    }

    /**
     * 查询未叫料的记录
     * @param callmatDosing
     * @return
     */
    @Override
    public JSONObject queryNotCallmatDosing(CallmatDosing callmatDosing) {
        Integer pageindex = callmatDosing.getPageindex();
        Integer pagesize = callmatDosing.getPagesize();
        if(pageindex!=null && pagesize!=null && !"".equals(pageindex) && !"".equals(pagesize)){
            callmatDosing.setIncept(pagesize * (pageindex - 1));
        }
        List<CallmatDto> callmatDosingDtoList = callmatDosingMapper.queryNotCallmatDosing(callmatDosing);
        if(!callmatDosingDtoList.isEmpty()){
            int countNotCallmatDosing = callmatDosingMapper.countNotCallmatDosing(callmatDosing);
            return JSONObject.oK("查询成功",callmatDosingDtoList,countNotCallmatDosing);
        }
        return JSONObject.oK("暂无数据",callmatDosingDtoList,null);

    }
    /**
     * 根据物料批号来获取到物料编号,物料名称
     * @param matNo
     * @return
     */
    @Override
    public JSONObject queryCallmat(String matNo) {
        CallmatDto callmatDosingDto=new CallmatDto();
        //这里改为了查询原材料
        Map<String, Mat> matMap = callmatDosingMapper.queryMat();
        if(StringUtils.isEmpty(matNo)){
            return JSONObject.build(JSONObject.ERROR,"物料批号为空",null);
        }
        for (Map.Entry<String, Mat> matEntry : matMap.entrySet()) {
            if(matNo.contains(matEntry.getKey()) || matEntry.getKey().equals(matNo)){
                callmatDosingDto.setMatCode(matEntry.getKey());
                callmatDosingDto.setMatName(matEntry.getValue().getMatName());
                callmatDosingDto.setMatId(matEntry.getValue().getMatId());
                break;
            }
        }
        if(StringUtils.isEmpty(callmatDosingDto.getMatCode())){
            return JSONObject.build(JSONObject.ERROR,"暂无数据",null);
        }
        return JSONObject.build(JSONObject.SUCCESS,"查询成功",callmatDosingDto);
    }

    public String checkParam(CallmatDosing callmatDosing){
        //物料id
        Integer matId = callmatDosing.getMatId();
        /*//叫料人id
        Integer callmatUserId = callmatDosing.getCallmatUserId();*/
        //需求数量
        String demandNum = callmatDosing.getDemandNum();

        if(matId==null || "".equals(matId)){
            return "未获取到物料id";
        }
        /*if(callmatUserId==null || "".equals(callmatUserId)){
            return "未获取到叫料人id";
        }*/
        if(StringUtils.isEmpty(demandNum)){
            return "未获取到需求数量";
        }else {
            if(StringUtils.isNumeric(demandNum)){
                if(Integer.parseInt(demandNum)==0 || "".equals(Integer.parseInt(demandNum))){
                    return "需求数量必须为整数并且不能为0";
                }
            }else{
                return "需求数量必须为整数";
            }
        }
        return null;
    }
}
